#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
. $basepath/common/common.sh

#0.检查执行用户是否是root
function checkRoot(){
	id | awk '{print $1}'|grep uid=0 >> /dev/null
	ASSERT $? "当前用户不是Root，无法运行这个脚本"
	echo "0. 确认当前用户是Root。"
}

#1.杀死java应用
function kill_java_apps(){
	echo "1. 开始杀死java应用(/usr/local/app)..."
	ps -ef|grep -v grep | grep /usr/local/app | awk '{print $2}' |xargs kill -9
	echo "#结束杀死java应用。"
}

#2.检查并启动Mysql
function startupMySQL(){
	echo "2. 开始检查并启动Mysql..."
	/etc/init.d/mysqld status >> /dev/null;
	if [[ ! 0 == $? ]]; then
		service mysqld start;
		ASSERT $? "MySQL没有启动,异常退出";
	fi
	echo "#MySQL 正确运行。"
}

#3.检查并启动Oracle
function startupOracle(){
	echo "3. 开始检查并启动Oracle..."
	ps -ef | grep ora_ | grep -v grep >> /dev/null
		if [[ ! 0 == $? ]]; then
			su - oracle <<EOF 

			lsnrctl start; 

			export ORACLE_SID=orcldb;
			sqlplus sys/ora as sysdba; 

			startup;

			exit;
EOF

		fi

	echo "#Oracle 正确运行。"	
}

#4.检查并启动cassandra
function startupCassandra(){
	echo "4. 开始检查并启动Cassandra..."
	/etc/init.d/cassandra status >> /etc/null
	if [[ ! 0 == $? ]]; then
		/etc/init.d/cassandra stop >> /etc/null;
		/etc/init.d/cassandra start >> /etc/null;
		ALERT $? "cassandra没有启动,异常退出";
	fi
	echo "#Cassandra 正确运行。"
}

#5.检查并启动redis
function startupRedis(){
	echo "5. 开始检查并启动Redis..."
	lsof -i:6379 >> /dev/null
	if [[ ! 0 == $? ]]; then
		 service redis1 stop
		 service redis1 start
		 ALERT $? "Redis1没有启动,异常退出";
	fi
	echo "    Redis 6379 正确运行。"


	lsof -i:16379 >> /dev/null
	if [[ ! 0 == $? ]]; then
		 service redis2 stop
		 service redis2 start
		 ALERT $? "Redis2没有启动,异常退出";
	fi
	echo "    Redis 16379 正确运行。"

	lsof -i:26379 >> /dev/null
	if [[ ! 0 == $? ]]; then
		 service redis3 stop
		 service redis3 start
		 ALERT $? "Redis3没有启动,异常退出";
	fi
	echo "    Redis 26379 正确运行。"

	lsof -i:36379 >> /dev/null
	if [[ ! 0 == $? ]]; then
		 service redis4 stop
		 service redis4 start
		 ALERT $? "Redis4没有启动,异常退出";
	fi
	echo "    Redis 36379 正确运行。"

	echo "#Redis 正确运行。"	
	redis-cli -p 6379 FLUSHALL;
	redis-cli -p 16379 FLUSHALL;
	redis-cli -p 26379 FLUSHALL;
	redis-cli -p 36379 FLUSHALL;
	echo "#Redis 缓存已清空。"
}

#6.检查并启动Zookeeper
function startupZookeeper(){
	echo "6. 开始检查并启动Zookeeper..."
	service zookeeper1 restart;

	service zookeeper2 restart;

	service zookeeper3 restart;

	service zookeeper4 restart;

	echo " Zookeeper 启动完毕。";	
}

#7.检查并启动rabbitmq
function startupRabbitmq(){
	echo "7. 开始检查并启动Rabbitmq..."
	/etc/init.d/rabbitmq-server status >> /etc/null
	if [[ ! 0 == $? ]]; then
		/etc/init.d/rabbitmq-server stop >> /etc/null;
		/etc/init.d/rabbitmq-server start >> /etc/null;
		ALERT $? "Rabbitmq没有启动,异常退出";
	fi
	echo "#Rabbitmq 正确运行。"
}

#8.检查并启动Mongodb
function startupMongodb(){
	echo "8. 开始检查并启动Mongodb..."
	lsof -i:27017 >> /dev/null
	if [[ ! 0 == $? ]]; then
		service mongodb start;
		ALERT $? "mondob没有启动,异常退出";
	fi
	echo "#Mongodb 正确运行。"	
}


#9.检查并启动httpd
function startupHttpd(){
	echo "9. 开始检查并启动Apache(httpd)..."
	/etc/init.d/httpd status >> /etc/null
	if [[ ! 0 == $? ]]; then
		echo "发现httpd没有启动，重新启动...";
		/etc/init.d/httpd restart;
		ALERT $? "httpd没有启动,异常退出";
	fi
	echo "#Apache(httpd) 正确运行。"	
}

#10.检查并启动vsftpd
function startupVsftpd(){
	echo "10. 开始检查并启动FTP(vsftpd)..."
	/etc/init.d/vsftpd status >> /etc/null
	if [[ ! 0 == $? ]]; then
		echo "发现httpd没有启动，重新启动.....";
		/etc/init.d/vsftpd restart;
		ALERT $? "vsftpd,异常退出";
	fi
	echo "#FTP(vsftpd) 正确运行。"	
}

#11.更新统一配置信息
function updateConfigs(){
	echo "11. 开始更新统一配置信息..."
	cd /usr/local/config/;
	svn update --accept theirs-full;
	cd ~;
	echo "#结束更新统一配置信息。"

}

#12.更新VM脚本
function checkoutVmScript(){
	echo "12. 开始更新VM脚本..."
	cd /usr/local/src/beetle-vm-init/;
	svn update --accept theirs-full;
	chmod 777 -R *;
	cd ~;
	echo "#结束脚本更新。"
}


function main(){

	checkRoot;

	kill_java_apps;

	startupMySQL;

	startupOracle;

	startupCassandra;
	
	startupRedis;

	startupZookeeper;

	startupRabbitmq;

	startupMongodb;

	startupHttpd;

	startupVsftpd;

	updateConfigs;

	checkoutVmScript;

	SUCESS_EXIT "所有中间件已经检查并启动完毕";

}

main;